import { createSlice } from '@reduxjs/toolkit';


const counterStore = createSlice({
    name: 'counter',
    // 初始状态变量
    initialState: {
        count: 0,
    },
    // 修改数据的同步方法
    reducers: {
        // 增加
        increase(state) {
            state.count++;
        },
        // 减少
        decrease(state) {
            state.count--;
        },
        // 支持传递参数
        addToNum(state, action) {
            state.count = action.payload;               // payload代表传递过来的参数
        }
    },
});

// 解构出actions
const { increase, decrease, addToNum } = counterStore.actions;

const counterReducer = counterStore.reducer;

// 导出
export { decrease, increase, addToNum };
export default counterReducer;